<?php
/**
 * （logic）创建基础备货JPsku提货（JP备货）
 */
namespace App\Logic\SsBasicJp;

use App\Models\Basics\SysPlat;
use App\Models\Basics\Transport;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductRakutenSku;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\JpSku;
use App\Models\SsBasic\JpSkuAvg;
use App\Models\SsBasic\JpSkuEstimate;
use App\Models\SsBasic\JpSkuStock;
use App\Models\SsBasic\JpStockingSkuRatio;
use Exception;
class initJpSyncSS
{
    /**
     * 同步JP备货基础数据 （country:sync-ss）
     * 设定：每天运行
     */
    public static function exe()
    {

        $amazon = SysPlat::PLAT_AMAZON;   //1:amazon  (亚马逊)
        $shopify = SysPlat::PLAT_SHOPIFY; //2:shopify (独立站)
        $rakuten = SysPlat::PLAT_RAKUTEN; //3:rakuten (乐天)

        $areaId = 6; //JP站点
        $countryId = 12; //JP国家
        $new = []; //JP-sku数据组

        //亚马逊sku
        $productAmazonSku = ProductAmazonSku::query()
            ->select('psku_id')
            ->where('country_id', $countryId)
            ->where('status',ProductAmazonSku::STATUS_YES)
            ->orderBy('psku_id')
            ->distinct()
            ->get();
        if ($productAmazonSku) {
            foreach($productAmazonSku as $amazonSku){
                $value = $amazon.'_'.$amazonSku->psku_id;
                if(!in_array($value,$new)){
                    $new[] = $value;
                }
            }
        }

        //乐天sku
        $productRakutenSku = ProductRakutenSku::query()
            ->select('psku_id')
            ->where('country_id', $countryId)
            ->where('status',ProductRakutenSku::STATUS_YES)
            ->orderBy('psku_id')
            ->distinct()
            ->get();
        if ($productAmazonSku) {
            foreach($productRakutenSku as $rakutenSku){
                $value = $rakuten.'_'.$rakutenSku->psku_id;
                if(!in_array($value,$new)){
                    $new[] = $value;
                }
            }
        }

        $new = $new ? array_unique($new) : []; //数组去重
        $count = count($new);
        if($count < 200){
            report(new Exception("Failed to artisan jp:sync-ss : count(" . $count . ")总数异常",121));
            die(0);
        }

        //需要删除的数据
        $old = JpSku::query()->pluck('area_psku')->toArray();
        $delete = array_diff($old,$new);
        foreach($delete as $_delete){
            list($platId,$pSkuId) = explode('_',$_delete);
            if($platId == $amazon) { //亚马逊
                $count = ProductAmazonSku::query()
                    ->where('status',ProductAmazonSku::STATUS_YES)
                    ->where('country_id',$countryId)
                    ->where('psku_id',$pSkuId)
                    ->count();
                if($count > 0)continue;
            }elseif($platId == $rakuten) {  //乐天
                $count = ProductRakutenSku::query()
                    ->where('status',ProductRakutenSku::STATUS_YES)
                    ->where('country_id',$countryId)
                    ->where('psku_id',$pSkuId)
                    ->count();
                if($count > 0)continue;
            }

            JPSku::query()->where('area_psku',$_delete)->delete(); //基础sku
            JpSkuAvg::query()->where('area_psku',$_delete)->delete(); //sku均值
            JpSkuStock::query()->where('area_psku',$_delete)->delete(); //sku库存
            JpSkuEstimate::query()->where('area_psku',$_delete)->delete(); //sku预估
            JpStockingSkuRatio::query()->where('area_psku',$_delete)->delete(); //sku销售比例
        }

        //需要新增的数据
        $add = array_diff($new,$old);
        $skuArr = [];
        $price = []; //采购价
        $pCycle = []; //生产周期
        $transportArr = []; //物流时效
        foreach($add as $_add){
            $count = JpSku::query()->where('area_psku',$_add)->count();
            if($count > 0)continue;
            list($platId,$pSkuId) = explode('_',$_add);
            if(!array_key_exists($pSkuId,$skuArr)){
                $skuArr[$pSkuId] = ProductSku::getModelById($pSkuId);
            }
            $sku = $skuArr[$pSkuId];
            if(!array_key_exists($pSkuId,$price)){
                $price[$pSkuId] = ProductSku::getPuPrice($sku);
            }
            if(!array_key_exists($pSkuId,$pCycle)){
                $pCycle[$pSkuId] = ProductSku::getPuCycle($sku);
            }
            if(!array_key_exists($platId,$transportArr)){
                $transportArr[$platId] = Transport::getTransportDay($areaId); //JP站点运输日期
            }
            $transport = $transportArr[$platId];


            //备货JPsku
            JpSku::addJpSku($platId,$pSkuId,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle[$pSkuId],$price[$pSkuId]);
        }
    }
}
